Failed Conditions
Pull Request — master (#2)
by Yo
01:30
created

index.js ➔ ???   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 3
c 5
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 9.4285
1
"use strict";
2
3
if (['dev', 'prod'].indexOf(process.env.NODE_ENV)) {
4
    console.warn(`Undefined node environment "${process.env.NODE_ENV}", fallback to dev !`);
5
    process.env.NODE_ENV = 'dev';
6
}
7
8
const onDeath = require('death')({
9
    SIGTERM: false,
10
    SIGQUIT: false
11
});
12
13
const logger = require('./lib/logger');
14
const taskLoggerFactory = require('./lib/logger/taskLogger');
15
const server = require('./lib/server');
16
const pkg = require('./package.json');
17
const NestedError = require('nested-error-stacks');
18
19
const appName = pkg.name;
20
const taskLogger = taskLoggerFactory(appName);
21
22
onDeath(signal => {
23
    let exitCode = 0;
24
    if (signal === 'SIGTERM' || signal === 'SIGQUIT') {
25
        exitCode = 1;
26
    }
27
28
    console.log('ON_DEATH');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
29
30
    cleanAndExit(exitCode);
31
});
32
33
process.on('unhandledRejection', (reason, p) => {
34
    console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
35
36
    return cleanAndExit(1);
37
});
38
39
const cleanAndExit = (exitCode = 0) => {
40
    taskLogger.stopping();
41
    const _exit = error => {
42
        if (error instanceof Error) {
43
            console.error(error);
44
        }
45
        taskLogger.stopped();
46
        process.exit(exitCode);
1 ignored issue
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
47
    };
48
49
    return server.stop()
50
        .then(_exit, _exit);
51
};
52
53
taskLogger.starting();
54
55
server.start()
56
    .then(() => taskLogger.started())
57
    .catch(error => {
58
        const newError = new NestedError(`Exit ${appName} after an error at initialisation`, error);
59
        logger.error(newError.stack);
60
61
        return Promise.reject(cleanAndExit(1));
62
    });
63